﻿using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ManagedUIFlow.Infrastructure.Commands.FlowActionHelpers;
using ManagedUIFlow.Infrastructure.Services;
using ManagedUIFlow.Model;
using Moq;
using Xunit;

namespace ManagedUIFlow.Specs.Infrastructure.Commands.FlowActionHelpers
{
    public class FlowActionResultSpecs
    {
        [Fact]
        public void ExecuteCommand_SetsStateAndTransitionTheFlow()
        {
            const string state = "someState";
            var flow = new ManagedFlow("foo");
            var flowServiceMock = new Mock<FlowService>();            
            var appControllerMock = new Mock<ApplicationController>();
            var contextMock = new Mock<ControllerContext>();
            var controllerMock = new Mock<ControllerBase>();            
            contextMock.Setup(c => c.Controller).Returns(controllerMock.Object);

            new FlowActionResult(flow, state)
                {
                    ApplicationController = appControllerMock.Object,
                    FlowService = flowServiceMock.Object
                }.ExecuteResult(contextMock.Object);

            Assert.Same(controllerMock.Object.ViewData, flow.ViewData);
            Assert.Same(controllerMock.Object.TempData, flow.TempData);
            flowServiceMock.Verify(r => r.GetNavigationFor(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()));
            appControllerMock.Verify(a => a.Execute(contextMock.Object.RequestContext, It.IsAny<ManagedFlow>()));
        }
    }
}